<?php
namespace Message\Controller;
use Common\Controller\MessageController;
class NewSongController extends MessageController {

    const APPID="wx370386b7eb0c04bc";
    const SECRET="25d7798f008b06ec3641d5e709599600";

    /**
     * 缓存到文件
     */
    private function cachenewsong(){

        exit;

        if (S('cachenewsong')){
            echo count(S('cachenewsong'));
        }
        $info = M('newsong_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where(array('status'=>'-1'))->order('wxopen_template_send_id asc')->select();

        $data = array();
        foreach ($info as $key=>$value){

            $data[$value['touser']] = $value;

        }

        S('cachenewsong',$info,7200*24);

        echo count(S('cachenewsong'));

    }

    public function test(){

        exit();
        $cache = S('cachenewsong');
        foreach ($cache as $value){

            $info = M('newsong_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->find();
            if ($info['createtime']<'2018-06-13'){
                $data[] = $info['uid'];
            }

        }

        S('lishi',$data);

        var_dump($data);


    }


    public function songtoken(){

        $this->gettoken2(self::APPID,self::SECRET);
    }

    public function delsong(){

        exit();
        $where['status'] = array('neq','-1');
        M('newsong_template_send')->where($where)->delete();
        exit('ok');

        echo 'ok';

    }


    /**
     * 新版猜歌
     */
    public function sendnewsong($start,$end){

        exit();
       $info = S('cachenewsong');
//        $where['status'] = '-1';
//        $where['touser'] = 'oOlSP4i-TrdOW3iAaGHxo0Ip5E0A';
//        $info = M('newsong_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where($where)->limit(1)->order('wxopen_template_send_id asc')->select();

        $appid = "wx370386b7eb0c04bc";
        $secret = "25d7798f008b06ec3641d5e709599600";
        $temp = "lNgznJEBaRp2armHoa2PFathIiVaR66U1pXCytifCo4";

        $name = $appid.'access_token';
        $access_token = S($name);

        foreach ($info as $key=>$value){

        if ($key>=$start&&$key<=$end) {
            $fdata = array(
                'keyword1' => array(
                    'value' => '千元大奖已送出 ',
                    'color' => '#ff0000',
                ),
                'keyword2' => array(
                    'value' => '现金大奖',
                    'color' => '#173177',
                ),
                'keyword3' => array(
                    'value' => '获奖者的现金大奖已发放完毕，请记得查收哦！注意事项  端午有奖活动圆满结束，谢谢朋友们的支持，没拿到奖的朋友们我们下次再约！',
                    'color' => '#173177',
                ),

            );

            $data = array(
                'touser' => $value['touser'],
                'template_id' => $temp,
                'page' => 'pages/index/index',
                'form_id' => $value['form_id'],
                'data' => $fdata,
                'emphasis_keyword' => 'keyword1.DATA'
            );
            $res = $this->sendtemplate2($access_token, $data);
            $save = array(
                'errcode' => $res['errcode'],
                'errmsg' => $res['errmsg'],
                'data' => json_encode($fdata),
                'emphasis_keyword' => 'keyword1.DATA',
            );
            echo $res['errcode'];
            if ($res['errcode'] == 0) {
                $save['status'] = 1;
                $save['send_time'] = time();
                M('newsong_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            } else {
                $save['status'] = 2;
                M('newsong_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }
        }

        }
        echo 'ok';

    }

}